import { ref } from 'vue'
/**  
 * @param 获取屏幕高度
 **/
export const getWindowHeight = (isTabber = false) : number => {
	let setWindowHeight : number = 0
	// getSystemInfo异步   getSystemInfoSync同步
	uni.getSystemInfo({
		success: (res) => {
			console.log('当前手机的机型数据', res)
			// #ifdef MP-WEIXIN
			setWindowHeight = res.windowHeight;
			// #endif

			// #ifdef H5
			if (isTabber) {
				// console.log('111111111111111',isTabber);
				if (res.windowHeight === res.screenHeight) {
					setWindowHeight = res.windowHeight - 44 - 50;
				} else {
					setWindowHeight = res.windowTop === 0 ? res.windowHeight - 44 : res.windowHeight;
				}
			} else {
				// console.log('22222222222222',isTabber);
				setWindowHeight = res.windowTop === 0 ? res.windowHeight - 44 : res.windowHeight;
			}
			// setWindowHeight = isTabber ? res.windowHeight - 44 - 50 :  res.windowHeight - 44;
			// #endif
		}
	})
	return setWindowHeight
}
/**  
 * @param _className类名
 * @param  isTabber是否底部有tabber底部栏
 * **/
// 有头部悬浮的滚动高度
export const getScrollHeight = (_className : string, isTabber = false) => {
	//	resolve reject
	return new Promise(function (resolve) {
		let tempNav : number = 0;
		let tempWindowHeight : number = getWindowHeight() // 获取屏幕高度
		setTimeout(() => {
			let query:any = ref(null)
			// #ifdef MP-WEIXIN
			query = wx.createSelectorQuery();
			// #endif

			// #ifndef MP-WEIXIN
			query = uni.createSelectorQuery().in(this);
			// #endif

			// console.log('H5query',query)  // data:UniApp.NodeInfo
			query.select(_className).boundingClientRect((data : any) => {
				tempNav = data.height;
				console.log('屏幕高度：', tempWindowHeight, '头部悬浮高度', tempNav)
				if (tempNav > 0) {
					let windowHeight : number = 0;
					// #ifdef MP-WEIXIN
					windowHeight = tempWindowHeight - tempNav;
					// #endif

					// #ifndef MP-WEIXIN
					if (isTabber) {
						windowHeight = tempWindowHeight - tempNav - 50;
					} else {
						windowHeight = tempWindowHeight - tempNav
					}
					// #endif
					resolve(windowHeight)
				}
			}).exec();
		})
	})
}

/**  
 * @param 获取当前元素的高度
 * @param _className类名
 * **/
export const getElementHeight = (_className : string):Promise<number> => {
	let that = this
	//	resolve reject
	return new Promise(function (resolve) {
		setTimeout(() => {
			let query = uni.createSelectorQuery().in(that);
			query.select(_className).boundingClientRect((data : any) => {
				resolve(data.height)
			}).exec();

		})
	})
}